package com.mingyizhudao.app.view.login;

import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mobstat.StatService;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.utils.Utils;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.http.DataUtils;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.view.MYBaseActivity;
public class LoginActivity extends MYBaseActivity {

	private EditText et_mobile, et_smscode;
	private Button btn_getsmscode;
	private TimeCount timeCount;
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == Constants.Result_Status.RESULT_SCCESS){
				Toast.makeText(activity, "验证码已发送，注意查收！", 0)
				.show();
			}else if(msg.what == Constants.Result_Status.RESULT_FAILURE){
				
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initView();
	}

	@Override
	public void initHeaderView() {
		
	}
	
	private void initView() {
		et_mobile = (EditText) findViewById(R.id.et_mobile);
		et_smscode = (EditText) findViewById(R.id.et_smscode);
		btn_getsmscode = (Button) findViewById(R.id.btn_getsmscode);
		
		timeCount = new TimeCount(60000, 1000);// 构造CountDownTimer对象

	}
	
	public void onBack(View view) {
		finish();
	}
	
	public void onSmsCode(View view) {
		if(Utils.checkMobile(et_mobile)){
			timeCount.start();// 开始计时
			String mobile = et_mobile.getText().toString().trim();
			Map params = new HashMap<String , Object>();
			params.put("smsVerifyCode[mobile]", mobile);
			params.put("smsVerifyCode[action_type]", 102);
			
			String url = CommonUtils.getSendCodeUrl(activity);
			postHttpResult(url, params, Constants.HTTP_INDEX.First, new TypeToken<QjResult<Map<String, Object>>>() {});
		}
	}
	
	public void onLogin(View view) {
		String mobile = et_mobile.getText().toString().trim();
		if (Utils.checkMobile(mobile)) {
			String sysCode = et_smscode.getText().toString().trim();
			if(Utils.isStringEmpty(sysCode)){
				et_smscode.setFocusable(true);
				Toast.makeText(activity, "请输入验证码！", 0).show();
				return;
			}
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("userLogin[username]", mobile);
			params.put("userLogin[verify_code]", sysCode);// 验证码
			String url = CommonUtils.getLoginUrl(activity);
			postHttpResult(url, params, Constants.HTTP_INDEX.Second, new TypeToken<QjResult<Map<String, Object>>>() {});
			CommonUtils.setMobile(activity, mobile);
			return;
		}
		et_mobile.requestFocus();
		Toast.makeText(activity, "请核对手机号！", 0).show();
	}

	
	@Override
	public void onSuccess(QjResult result, String flg) {
		super.onSuccess(result, flg);
		if(flg.equals(Constants.HTTP_INDEX.First)){
			Toast.makeText(activity, "验证码已发送，注意查收！", 0)
			.show();
		}else if(flg.equals(Constants.HTTP_INDEX.Second)){
			Map<String, Object> map = (Map<String, Object>) result.getResults();
			if(map != null && !map.isEmpty()){
				String token = map.get("token")+"";
				Object object = map.get("is_new_user");
				if(object != null){
				    double is_new_user = Double.parseDouble(object+""); 
					if(is_new_user == 2){
						StatService.onEvent(activity, "register", "eventLabel", 1);
					}
				}
				CommonUtils.setToken(activity, token);
				finish();
			}
		}
	}
	
	@Override
	public void onFailure(QjResult result, String flg) {
		super.onFailure(result, flg);
		
		if(flg.equals(Constants.HTTP_INDEX.First)){
			
			handler.post(new Runnable() {
				
				@Override
				public void run() {
					Toast.makeText(activity, "验证码发送失败！", 0).show();
					timeCount.cancel();
					btn_getsmscode.setText("获取验证码");
					btn_getsmscode.setClickable(true);
					btn_getsmscode
							.setBackgroundColor(getResources()
									.getColor(
											R.color.btn_blue_color));					
				}
			});
		}
	}
	
	@Override
	public void onCompleted(Exception e, String flg) {
		super.onCompleted(e, flg);
		LoadingDialog.getInstance(activity).dismiss();
	}
	

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			btn_getsmscode.setText("获取验证码");
			btn_getsmscode.setClickable(true);
			btn_getsmscode.setBackgroundColor(getResources().getColor(
					R.color.white));
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			btn_getsmscode.setClickable(false);
			btn_getsmscode.setBackgroundColor(getResources().getColor(
					R.color.actionbar_bg_color));

			btn_getsmscode.setText(millisUntilFinished / 1000 + "秒后重发");
		}
	}
}
